using System.Collections;
using UnityEngine;

public class TRB_Hospital_IntroductionCutscene : MonoBehaviour
{
	[Header("-= Cutscene References =-")]
	public PlayerManager Kris;

	public Animator Susie;

	public Animator Berdly;

	public Animator Noelle;

	public Animator Nurse;

	public SpriteRenderer Receptionist;

	public SpriteRenderer Fade_Hacky;

	[SerializeField]
	private TRB_Hospital_FlashbackSequence FlashbackSequence;

	[Header("-= Cutscene Chats =-")]
	[SerializeField]
	private CHATBOXTEXT[] CutsceneChats;

	[SerializeField]
	private AudioClip[] CutsceneSounds;

	[SerializeField]
	private bool Noelle_ContinuePacing = true;

	[SerializeField]
	private bool Noelle_HasStoppedPacing;

	private Coroutine Coroutine_NoellePace;

	private Coroutine Coroutine_Cutscene;

	private void Start()
	{
		Kris = PlayerManager.Instance;
		Kris._PlayerState = PlayerManager.PlayerState.Cutscene;
		Kris._PMove.AnimationOverriden = true;
		LightworldMenu.Instance.CanOpenMenu = false;
		DarkworldMenu.Instance.CanOpenMenu = false;
		Susie.SetBool("InCutscene", value: true);
		Berdly.SetBool("InCutscene", value: true);
		Noelle.SetBool("InCutscene", value: true);
		Kris._PMove._anim.SetBool("MOVING", value: true);
		Coroutine_NoellePace = StartCoroutine(NoellePaceLoop());
		Coroutine_Cutscene = StartCoroutine(Cutscene());
	}

	private void Update()
	{
		if (Input.GetKeyDown(KeyCode.L) && DRHDebugManager.instance.DebugModeEnabled)
		{
			StopCoroutine(Coroutine_NoellePace);
			StopCoroutine(Coroutine_Cutscene);
			FlashbackSequence.Debug_IsPlayingFinale = true;
			CameraManager.instance.transform.position = new Vector3(60f, 30f, -10f);
			FlashbackSequence.gameObject.SetActive(value: true);
			FlashbackSequence.StartCoroutine("Cutscene_Finale");
		}
	}

	private IEnumerator Cutscene()
	{
		yield return null;
		Berdly.Play("WalkN");
		Susie.Play("WalkSad");
		Kris._PMove._anim.SetBool("MOVING", value: true);
		RotateSusieToDirection(Vector2.up);
		RotateBerdlyToDirection(Vector2.up);
		RotateKrisToDirection(Vector2.up);
		CutsceneUtils.MoveTransformLinear(Kris.transform, new Vector2(0f, -4.3f), 1f);
		CutsceneUtils.MoveTransformLinear(Susie.transform, new Vector2(0f, -4.8f), 1f);
		CutsceneUtils.MoveTransformLinear(Berdly.transform, new Vector2(0f, -5.3f), 1f);
		yield return new WaitForSeconds(1f);
		CutsceneUtils.MoveTransformLinear(Kris.transform, new Vector2(0f, -2.65f), 1f);
		CutsceneUtils.MoveTransformLinear(Susie.transform, new Vector2(-1.2f, -3.85f), 1.4f);
		CutsceneUtils.MoveTransformLinear(Berdly.transform, new Vector2(1.2f, -3.85f), 1.4f);
		yield return new WaitForSeconds(1.3f);
		Kris._PMove._anim.SetBool("MOVING", value: false);
		Berdly.Play("IdleN");
		Susie.Play("IdleSad");
		Kris._PMove.AnimationOverriden = false;
		yield return new WaitForSeconds(0.5f);
		CutsceneUtils.RunFreshChat(CutsceneChats[0], 0, ForcePosition: true, OnBottom: false);
		while (ChatboxManager.Instance.ChatIsCurrentlyRunning)
		{
			yield return null;
		}
		Noelle_ContinuePacing = false;
		while (!Noelle_HasStoppedPacing)
		{
			yield return null;
		}
		CutsceneUtils.RunFreshChat(CutsceneChats[0], 1, ForcePosition: true, OnBottom: false);
		while (ChatboxManager.Instance.ChatIsCurrentlyRunning)
		{
			yield return null;
		}
		yield return new WaitForSeconds(1f);
		CutsceneUtils.RunFreshChat(CutsceneChats[0], 2, ForcePosition: true, OnBottom: false);
		while (ChatboxManager.Instance.ChatIsCurrentlyRunning)
		{
			yield return null;
		}
		yield return new WaitForSeconds(1f);
		CutsceneUtils.RunFreshChat(CutsceneChats[0], 3, ForcePosition: true, OnBottom: false);
		while (ChatboxManager.Instance.ChatIsCurrentlyRunning)
		{
			yield return null;
		}
		yield return new WaitForSeconds(0.35f);
		CutsceneUtils.FadeInSprite(Fade_Hacky, 2f);
		yield return new WaitForSeconds(0.5f);
		yield return null;
		Noelle.Play("TRB_HospitalSeated_Sad");
		Noelle.transform.position = new Vector2(-3.6f, -2.85f);
		RotateSusieToDirection(Vector2.left);
		Susie.transform.position = new Vector2(-1.4f, -3.3f);
		RotateBerdlyToDirection(Vector2.up);
		Berdly.transform.position = new Vector2(-2.65f, -4f);
		RotateKrisToDirection(Vector2.down);
		Kris.transform.position = new Vector2(-2.6f, -0.95f);
		yield return null;
		CutsceneUtils.FadeOutSprite(Fade_Hacky, 2f);
		yield return new WaitForSeconds(1.5f);
		CutsceneUtils.RunFreshChat(CutsceneChats[1], 0, ForcePosition: true, OnBottom: false);
		while (ChatboxManager.Instance.ChatIsCurrentlyRunning)
		{
			yield return null;
		}
		yield return new WaitForSeconds(1f);
		CutsceneUtils.RunFreshChat(CutsceneChats[1], 1, ForcePosition: true, OnBottom: false);
		while (ChatboxManager.Instance.ChatIsCurrentlyRunning)
		{
			yield return null;
		}
		yield return new WaitForSeconds(0.5f);
		CutsceneUtils.RunFreshChat(CutsceneChats[1], 2, ForcePosition: true, OnBottom: false);
		while (ChatboxManager.Instance.ChatIsCurrentlyRunning)
		{
			yield return null;
		}
		yield return new WaitForSeconds(1f);
		Nurse.gameObject.SetActive(value: true);
		Nurse.SetBool("InCutscene", value: true);
		CutsceneUtils.RotateCharacterToDirection(Nurse, "VelocityX", "VelocityY", Vector2.down);
		CutsceneUtils.PlaySound(CutsceneSounds[0]);
		Receptionist.flipX = false;
		yield return new WaitForSeconds(1f);
		Nurse.Play("WalkSad");
		CutsceneUtils.MoveTransformLinear(Nurse.transform, new Vector2(-2.875f, 0.025f), 0.45f);
		CutsceneUtils.MoveTransformLinear(Receptionist.transform, new Vector2(-0.775f, 1.975f), 0.75f);
		yield return new WaitForSeconds(0.45f);
		CutsceneUtils.MoveTransformLinear(Nurse.transform, new Vector2(-2.525f, 0.025f), 0.1f);
		Nurse.Play("IdleSad");
		CutsceneUtils.RotateCharacterToDirection(Nurse, "VelocityX", "VelocityY", Vector2.right);
		yield return new WaitForSeconds(0.3f);
		yield return new WaitForSeconds(1f);
		CutsceneUtils.RotateCharacterToDirection(Nurse, "VelocityX", "VelocityY", Vector2.down);
		Receptionist.flipX = true;
		CutsceneUtils.RunFreshChat(CutsceneChats[1], 4, ForcePosition: true, OnBottom: true);
		Noelle.Play("TRB_HospitalSeated_Turned");
		RotateKrisToDirection(Vector2.up);
		RotateSusieToDirection(Vector2.up);
		while (ChatboxManager.Instance.ChatIsCurrentlyRunning)
		{
			yield return null;
		}
		yield return new WaitForSeconds(0.5f);
		CutsceneUtils.RunFreshChat(CutsceneChats[1], 5, ForcePosition: true, OnBottom: false);
		RotateKrisToDirection(Vector2.down);
		RotateSusieToDirection(Vector2.left);
		while (ChatboxManager.Instance.ChatIsCurrentlyRunning)
		{
			yield return null;
		}
		yield return new WaitForSeconds(0.35f);
		CutsceneUtils.RotateCharacterToDirection(Nurse, "VelocityX", "VelocityY", Vector2.right);
		Receptionist.flipX = false;
		Noelle.Play("IdleN");
		RotateNoelleToDirection(Vector2.up);
		Noelle.transform.position = new Vector2(-3.4f, -2.8f);
		CutsceneUtils.PlaySound(CutsceneSounds[1]);
		yield return new WaitForSeconds(0.35f);
		Noelle.Play("WalkN");
		Noelle.speed = 0.75f;
		CutsceneUtils.MoveTransformLinear(Noelle.transform, new Vector3(-3.4f, -0.78f), 2.5f);
		StartCoroutine(KrisAvoidNoelleIntersect());
		yield return new WaitForSeconds(2.5f);
		yield return null;
		Noelle.Play("TRB_Hesitant");
		Noelle.speed = 1f;
		CutsceneUtils.ShakeTransform(Noelle.transform);
		CutsceneUtils.PlaySound(CutsceneSounds[2]);
		yield return new WaitForSeconds(1.5f);
		CutsceneUtils.RunFreshChat(CutsceneChats[1], 6, ForcePosition: true, OnBottom: false);
		while (ChatboxManager.Instance.ChatIsCurrentlyRunning)
		{
			yield return null;
		}
		yield return new WaitForSeconds(0.35f);
		CutsceneUtils.RunFreshChat(CutsceneChats[1], 7, ForcePosition: true, OnBottom: false);
		while (ChatboxManager.Instance.ChatIsCurrentlyRunning)
		{
			yield return null;
		}
		Noelle.Play("WalkN");
		Noelle.speed = 0.75f;
		CutsceneUtils.MoveTransformLinear(Noelle.transform, new Vector3(-3.4f, 0.55f), 0.75f);
		yield return new WaitForSeconds(0.75f);
		Noelle.gameObject.SetActive(value: false);
		CutsceneUtils.PlaySound(CutsceneSounds[0]);
		yield return new WaitForSeconds(1.35f);
		CutsceneUtils.RunFreshChat(CutsceneChats[1], 8, ForcePosition: true, OnBottom: false);
		while (ChatboxManager.Instance.ChatIsCurrentlyRunning)
		{
			yield return null;
		}
		CutsceneUtils.FadeInSprite(Fade_Hacky);
		yield return new WaitForSeconds(1f);
		CutsceneUtils.FadeOutSprite(Fade_Hacky, 10f);
		CameraManager.instance.transform.position = new Vector3(-30f, 0f, -10f);
		Noelle.transform.position = new Vector2(30f, -3.7f);
		yield return new WaitForSeconds(1f);
		CutsceneUtils.RunFreshChat(CutsceneChats[1], 9, ForcePosition: true, OnBottom: false);
		while (ChatboxManager.Instance.ChatIsCurrentlyRunning)
		{
			yield return null;
		}
		CutsceneUtils.FadeInSprite(Fade_Hacky, 10f);
		yield return new WaitForSeconds(1f);
		CameraManager.instance.transform.position = new Vector3(30f, 0f, -10f);
		CutsceneUtils.FadeOutSprite(Fade_Hacky, 2f);
		FlashbackSequence.gameObject.SetActive(value: true);
	}

	public IEnumerator Cutscene_Aftermath()
	{
		CutsceneUtils.FadeInSprite(Fade_Hacky);
		yield return new WaitForSeconds(1f);
		Noelle.transform.position = new Vector3(-3.4f, 0.55f);
		Nurse.gameObject.SetActive(value: false);
		Noelle.GetComponent<SpriteRenderer>().color = Color.white;
		Noelle.Play("IdleDepressed");
		Noelle.GetComponent<SpriteRenderer>().enabled = false;
		RotateNoelleToDirection(Vector2.down);
		Receptionist.transform.position = new Vector2(1.675f, 1.975f);
		Receptionist.flipX = true;
		Kris.transform.position = new Vector2(-3.15f, -3.05f);
		Berdly.transform.position = new Vector2(-3.15f, -2.15f);
		Susie.transform.position = new Vector2(-1.95f, -3.95f);
		RotateKrisToDirection(Vector2.up);
		RotateSusieToDirection(Vector2.up);
		RotateBerdlyToDirection(Vector2.down);
		CameraManager.instance.transform.position = new Vector3(0f, 0f, -10f);
		yield return null;
		CutsceneUtils.FadeOutSprite(Fade_Hacky);
		yield return new WaitForSeconds(2.5f);
		CutsceneUtils.RunFreshChat(CutsceneChats[2], 0, ForcePosition: true, OnBottom: false);
		while (ChatboxManager.Instance.ChatIsCurrentlyRunning)
		{
			yield return null;
		}
		yield return new WaitForSeconds(0.35f);
		Noelle.GetComponent<SpriteRenderer>().enabled = true;
		CutsceneUtils.PlaySound(CutsceneSounds[0]);
		yield return new WaitForSeconds(1f);
		Noelle.Play("WalkDepressed");
		CutsceneUtils.MoveTransformLinear(Noelle.transform, new Vector2(-3.4f, -1.25f), 3f);
		yield return new WaitForSeconds(3f);
		Noelle.Play("IdleDepressed");
		yield return new WaitForSeconds(1f);
		CutsceneUtils.RunFreshChat(CutsceneChats[2], 1, ForcePosition: true, OnBottom: false);
		while (ChatboxManager.Instance.ChatIsCurrentlyRunning)
		{
			yield return null;
		}
		yield return new WaitForSeconds(0.5f);
		RotateNoelleToDirection(Vector2.right);
		CutsceneUtils.PlaySound(CutsceneSounds[3]);
		yield return new WaitForSeconds(2f);
		CutsceneUtils.RunFreshChat(CutsceneChats[2], 2, ForcePosition: true, OnBottom: false);
		while (ChatboxManager.Instance.ChatIsCurrentlyRunning)
		{
			yield return null;
		}
		yield return new WaitForSeconds(0.5f);
		RotateNoelleToDirection(Vector2.up);
		Noelle.Play("WalkDepressed");
		CutsceneUtils.MoveTransformLinear(Noelle.transform, new Vector2(-3.4f, 0.55f), 2f);
		yield return new WaitForSeconds(2f);
		Noelle.Play("IdleDepressed");
		CutsceneUtils.RunFreshChat(CutsceneChats[2], 3, ForcePosition: true, OnBottom: false);
		while (ChatboxManager.Instance.ChatIsCurrentlyRunning)
		{
			yield return null;
		}
		yield return new WaitForSeconds(2f);
		CutsceneUtils.RunFreshChat(CutsceneChats[2], 4, ForcePosition: true, OnBottom: true);
		while (ChatboxManager.Instance.ChatIsCurrentlyRunning)
		{
			yield return null;
		}
		yield return new WaitForSeconds(0.75f);
		Noelle.gameObject.SetActive(value: false);
		CutsceneUtils.PlaySound(CutsceneSounds[0]);
		yield return new WaitForSeconds(3f);
		CutsceneUtils.RunFreshChat(CutsceneChats[2], 5, ForcePosition: true, OnBottom: false);
		while (ChatboxManager.Instance.ChatIsCurrentlyRunning)
		{
			yield return null;
		}
		CutsceneUtils.FadeInSprite(Fade_Hacky);
		yield return new WaitForSeconds(1f);
		CameraManager.instance.transform.position = new Vector3(60f, 30f, -10f);
		CutsceneUtils.FadeOutSprite(Fade_Hacky, 2f);
		yield return new WaitForSeconds(2f);
		FlashbackSequence.gameObject.SetActive(value: true);
		FlashbackSequence.StartCoroutine("Cutscene_Finale");
	}

	private IEnumerator KrisAvoidNoelleIntersect()
	{
		Kris.transform.position = new Vector2(-2.6f, -0.95f);
		CutsceneUtils.MoveTransformLinear(Kris.transform, new Vector2(-2f, -0.95f), 0.4f);
		RotateKrisToDirection(Vector2.left);
		Kris._PMove._anim.SetBool("MOVING", value: true);
		yield return new WaitForSeconds(0.4f);
		Kris._PMove._anim.SetBool("MOVING", value: false);
		yield return new WaitForSeconds(0.5f);
		RotateKrisToDirection(Vector2.up);
		RotateSusieToDirection(Vector2.up);
	}

	private IEnumerator NoellePaceLoop()
	{
		Vector2 leftPos = new Vector2(-2.85f, -1.25f);
		Vector2 rightPos = new Vector2(2.85f, -1.25f);
		Vector2 centerPos = new Vector2(0f, -1.25f);
		bool movingRight = false;
		while (true)
		{
			Vector2 targetPos = (movingRight ? rightPos : leftPos);
			Vector2 direction = (movingRight ? Vector2.right : Vector2.left);
			Noelle.Play("TRB_WalkParanoid");
			RotateNoelleToDirection(direction);
			CutsceneUtils.MoveTransformLinear(Noelle.transform, centerPos, 1f);
			yield return new WaitForSeconds(1f);
			while (!Mathf.Approximately(Noelle.transform.position.x, 0f))
			{
				yield return null;
			}
			if (!Noelle_ContinuePacing)
			{
				break;
			}
			Noelle.Play("TRB_WalkParanoid");
			RotateNoelleToDirection(direction);
			CutsceneUtils.MoveTransformLinear(Noelle.transform, targetPos, 1f);
			yield return new WaitForSeconds(1f);
			Noelle.Play("TRB_IdleParanoid");
			CutsceneUtils.ShakeTransform(Noelle.transform, 0.2f, 1f);
			yield return new WaitForSeconds(1f);
			movingRight = !movingRight;
		}
		Noelle.Play("TRB_IdleParanoid");
		Noelle_HasStoppedPacing = true;
	}

	private void RotateSusieToDirection(Vector2 direction)
	{
		CutsceneUtils.RotateCharacterToDirection(Susie, "VelocityX", "VelocityY", direction);
	}

	private void RotateBerdlyToDirection(Vector2 direction)
	{
		CutsceneUtils.RotateCharacterToDirection(Berdly, "VelocityX", "VelocityY", direction);
	}

	private void RotateNoelleToDirection(Vector2 direction)
	{
		CutsceneUtils.RotateCharacterToDirection(Noelle, "VelocityX", "VelocityY", direction);
	}

	private void RotateKrisToDirection(Vector2 direction)
	{
		PlayerManager.Instance._PMove.RotatePlayerAnim(direction);
	}

	public void SusieIdle()
	{
		Susie.Play("Idle");
	}

	public void SusieLeft()
	{
		RotateSusieToDirection(Vector2.left);
	}

	public void SusieUp()
	{
		RotateSusieToDirection(Vector2.up);
	}

	public void SusieRight()
	{
		RotateSusieToDirection(Vector2.right);
	}

	public void SusieDown()
	{
		RotateSusieToDirection(Vector2.down);
	}

	public void SusieIdleSad()
	{
		Susie.Play("IdleSad");
	}

	public void BerdlyLeft()
	{
		RotateBerdlyToDirection(Vector2.left);
	}

	public void BerdlyUp()
	{
		RotateBerdlyToDirection(Vector2.up);
	}

	public void BerdlyRight()
	{
		RotateBerdlyToDirection(Vector2.right);
	}

	public void BerdlyDown()
	{
		RotateBerdlyToDirection(Vector2.down);
	}

	public void BerdlyIdle()
	{
		Berdly.Play("IdleN");
	}

	public void KrisUp()
	{
		RotateKrisToDirection(Vector2.up);
	}

	public void KrisRight()
	{
		RotateKrisToDirection(Vector2.right);
	}

	public void KrisLeft()
	{
		RotateKrisToDirection(Vector2.left);
	}

	public void KrisDown()
	{
		RotateKrisToDirection(Vector2.down);
	}

	public void Noelle_HospitalSeated_Sad()
	{
		Noelle.Play("TRB_HospitalSeated_Sad");
	}

	public void Noelle_HospitalSeated_SadLookDown()
	{
		Noelle.Play("TRB_HospitalSeated_SadLookDown");
	}

	public void Noelle_HospitalSeated_SadLookRight()
	{
		Noelle.Play("TRB_HospitalSeated_SadLookRight");
	}

	public void Noelle_HospitalSeated_Happy()
	{
		Noelle.Play("TRB_HospitalSeated_Happy");
	}

	public void Noelle_HospitalSeated_Laugh()
	{
		Noelle.Play("TRB_HospitalSeated_Laugh");
	}

	public void Noelle_HospitalSeated_HappyLookDown()
	{
		Noelle.Play("TRB_HospitalSeated_HappyLookDown");
	}

	public void Noelle_HospitalSeated_HappyLookRight()
	{
		Noelle.Play("TRB_HospitalSeated_HappyLookRight");
	}

	public void Noelle_HospitalSeated_HappyLookBlush()
	{
		Noelle.Play("TRB_HospitalSeated_HappyBlush");
	}
}
